const app = getApp();
const request = (url, options) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: `${app.globalData.baseURL}${url}`,
      method: options.method,
      data: options.data,
      header: {
        'content-type': options.jsonType ? 'application/json' : 'application/x-www-form-urlencoded'
      },
      success(result) {
        console.log(result.data);
        if (result.data.code == 200) {
          resolve(result.data);
        } else {
          reject(result.data);
        }
      },
      fail(error) {
        reject(error)
      }
    })
  })
}
//增（默认给后端传json数据）
const post = (url, data, jsonType = true) => {
  return request(url, {
    data,
    jsonType,
    method: 'POST',
  })
}
//删
const del = (url, data, jsonType = true) => {
  return request(url, {
    data,
    jsonType,
    method: 'DELETE',
  })
}
//改
const put = (url, data, jsonType = true) => {
  return request(url, {
    data,
    jsonType,
    method: 'PUT',
  })
}
//查
const get = (url, data, jsonType = true) => {
  return request(url, {
    data,
    jsonType,
    method: 'GET',
  })
}
module.exports = {
  get,
  post,
  put,
  del
}